package com.tcd.explore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tcd.explore.Util.FtpUtil;
import com.tcd.explore.pojo.DataUploadInfo;
import com.tcd.explore.service.DataUploadInfoService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/generator")
public class DataUploadInfoController {
    @Value("${ftp.rootPath}")
    String path;
    @Value("${ftp.userName}")
    String username;
    @Value("${ftp.host}")
    String host;
    @Value("${ftp.port}")
    String port;
    @Value("${ftp.password}")
    String password;
    @Autowired
    DataUploadInfoService dataupload;

    @RequestMapping("/datauploadadd")
    public boolean datauploadadd(@RequestBody DataUploadInfo upload) {
        return dataupload.save(upload);
    }

    @PostMapping(value = "/dataupload")
    public boolean upload2(@RequestParam(value = "file") MultipartFile file) throws Exception {
        String filePath = new DateTime().toString("/yyyyMMdd/");
        //新文件名
        //原来的文件名
        String yfilename = file.getOriginalFilename();
        //文件后缀
        String suffix = file.getOriginalFilename().substring(yfilename.lastIndexOf("."));
        String path = "43.235.249.4/image/" + yfilename;
        System.out.println(path);
        InputStream input = null;
        try {
            input = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FtpUtil.putImages(input, yfilename);
        return true;
    }

    @RequestMapping("/datauploadone")
    public List<DataUploadInfo> Datauploadone(String claimMainid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("claim_main_id", claimMainid);
        return dataupload.list(wrapper);
    }

    @RequestMapping("/datauploaddel")
    public boolean datauploaddel(String dataid) {
        return dataupload.removeById(dataid);
    }
}
